<template>
  <div>
    <el-dialog title="欢迎登陆" :visible.sync="dialogVisible" width="30%">
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="用户名:">
          <el-input v-model="form.userName" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="密码:">
          <el-input v-model="form.password" show-password placeholder="请输入密码"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button @click="resetSubmit">取消</el-button>
          <el-button type="primary" @click="onSubmit" :loading="loading" :disabled="disabled">立即登陆</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <el-row type="flex" class="row-bg" justify="space-around">
      <el-col :span="8">
        <div>
          <h1>
            <el-link type="info" :underline="false">你好，</el-link>
            <a href="javascript:void(0)" target="_blank" v-if="userName">{{userName}}</a>
            <a href="javascript:void(0)" target="_blank" @click="loginFn" v-else>请登录</a>
          </h1>
          <p>欢迎使用备品备件管理系统。</p>
        </div>
      </el-col>
      <el-col :span="8" :offset="7">
        <div class="grid-content"></div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { mapActions, mapState } from 'vuex';

export default {
  name: 'login',
  data() {
    return {
      form: {
        userName: '',
        password: '',
      },
      // disabled: false,
      loading: false,
      dialogVisible: false,
      handleClose: false,
    };
  },
  created() {
    this.init();
  },
  computed: {
    ...mapState({
      userName: store => store.user.userName,
    }),
  },
  methods: {
    ...mapActions(['handleLogin', 'getUserInfo']),
    handleSubmit({ userName, password }) {
      console.log('调用登陆接口');
      this.handleLogin({ userName, password }).then(() => {
        this.getUserInfo().then(() => {
          this.$router.push({
            name: 'home',
          });
          this.dialogVisible = false;
        });
      });
    },
    init() {
      this.disabled = false;
      this.loading = false;
      this.dialogVisible = false;
    },
    loginFn() {
      this.dialogVisible = true;
    },
    onSubmit() {
      this.loading = true;
      this.handleSubmit({
        userName: this.form.userName,
        password: this.form.password,
      });
    },
    resetSubmit() {
      this.dialogVisible = false;
      this.init();
    },
  },
};
</script>
<style scoped>
h1,
a {
  line-height: 80px;
  font-size: 45px;
}
p {
  line-height: 80px;
  font-size: 25px;
}
.grid-content {
  height: 500px;
  border: 1px;
  background: url(./../assets/sps-logo.png);
  background-repeat: no-repeat;
  background-size: cover;
}
</style>
